AddCSLuaFile("cl_init.lua") 
AddCSLuaFile("shared.lua") 
include("shared.lua")

local CAMERA_MODEL = Model("models/dav0r/camera.mdl")

ENT.PhysShadowControl = {}
ENT.PhysShadowControl.secondstoarrive  = .01
ENT.PhysShadowControl.pos              = Vector(0, 0, 0)
ENT.PhysShadowControl.angle            = Angle(0, 0, 0)
ENT.PhysShadowControl.maxspeed         = 1000000
ENT.PhysShadowControl.maxangular       = 1000000
ENT.PhysShadowControl.maxspeeddamp     = 1000000
ENT.PhysShadowControl.maxangulardamp   = 1000000
ENT.PhysShadowControl.dampfactor       = 1
ENT.PhysShadowControl.teleportdistance = 0
ENT.PhysShadowControl.deltatime        = deltatime

function ENT:Initialize() 
	self.Entity:SetModel(CAMERA_MODEL)
	self.Entity:PhysicsInit(SOLID_VPHYSICS)
	self.Entity:SetMoveType(MOVETYPE_VPHYSICS)
	self.Entity:SetSolid(SOLID_NONE)
	
	self.Entity:DrawShadow(true)
	
	self.Entity:SetCollisionGroup(COLLISION_GROUP_WEAPON)
	
	self.Entity:SetColor(0, 0, 0, 100)
	
	self.Victims = {}
	
	local phys = self:GetPhysicsObject()
	
	if phys and phys:IsValid() then
		phys:Wake()
	end
	
	self.LastYaw = (self:GetAngles().y - 180) / 180
	
	return self:StartMotionController()
end

function ENT:KeyValue(k, v)
end

function ENT:UpdateTransmitState()
	return TRANSMIT_ALWAYS
end

function ENT:IsValidByFilter(ent, filter)
	if filter == 1 then -- any
		return true
	elseif filter == 2 then -- fire
		return (ent:IsOnFire() or string.find(ent:GetClass(), "_fire"))
	elseif filter == 3 then -- prop
		return (ent:GetClass() == "prop_physics")
	elseif filter == 4 then -- things with physics objects
		return (ent:GetPhysicsObject() and ent:GetPhysicsObject():IsValid())
	elseif filter == 5 then -- npcs
		return ent:IsNPC()
	elseif filter == 6 then -- players
		return ent:IsPlayer()
	elseif filter == 7 then -- light entities
		return (string.find(ent:GetClass(), "light") or (ent:GetClass() == "env_sprite"))
	elseif filter == 8 then -- 'info_particle_system's
		return (ent:GetClass() == "info_particle_system")
	end
end

function ENT:PhysicsSimulate(phys, deltatime)

	--if self.Entity:IsPlayerHolding() then return SIM_NOTHING end
	
	self.CatmullRomController:CalcPerc() -- Can't be done in the parameter call or a side effect doesn't manifest properly
	
	local CurPos  = self:GetPos()
	local CurNode = self.CatmullRomController.EntityList[self.CatmullRomController.CurSegment]
	
	phys:Wake()
	
	self.PhysShadowControl.pos = self.CatmullRomController:Point()
	self.PhysShadowControl.angle = self.CatmullRomController:Angle()

	self.PhysShadowControl.deltatime = deltatime
	
	return phys:ComputeShadowControl(self.PhysShadowControl)
end

function ENT:SetPlayers(players)
	if players.IsPlayer then -- Single player
		self.Victims[1] = players
	else
		self.Victims = players
	end
	
	for _, v in pairs(self.Victims) do
		if v.IsValid and v:IsValid() and v.SetViewEntity then
			v:SetViewEntity(self.Entity)
		end
	end
end

function ENT:OnRemove()
	self.CatmullRomController.CurSegmentTimestamp = 0
	self.CatmullRomController.CurSegment = 2
	
	for _, v in pairs(self.Victims) do
		if v.IsValid and v:IsValid() and v.SetViewEntity then
			v:SetViewEntity(v)
		end
	end
end
